package com.wiz.weixin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.wiz.weixin.TO.MemberPasswordTO;
import com.wiz.weixin.TO.PrPriceTO;
import com.wiz.weixin.VO.AuthVO;
import com.wiz.weixin.VO.MemberVO;
import com.wiz.weixin.common.Constant;
import com.wiz.weixin.manager.HttpManager;
import com.wiz.weixin.mapper.HqBaseInfoMapper;
import com.wiz.weixin.mapper.MemberPasswordMapper;
import com.wiz.weixin.mapper.PrPriceMapper;
import com.wiz.weixin.service.ScanRefuelingService;
import com.wiz.weixin.util.MD5Util;
/**
 * 扫一扫 加油服务
 * @author xxy
 *
 */
@Service("scanRefuelingService")
public class ScanRefuelingServiceImpl implements ScanRefuelingService {
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Resource
	private HqBaseInfoMapper hqBaseInfoMapper;
	@Resource
	private PrPriceMapper prPriceMapper;
	@Resource
	private HttpManager httpManager;
	@Resource
	private MemberPasswordMapper memberPasswordMapper;

	
	@Override
	public Map<String,Object> hqbyFpId(String fpId,String openId)throws Exception {
		Map<String,Object> map = new HashMap<String, Object>();
		PrPriceTO prPriceTO = null;
		
		List<PrPriceTO>list = prPriceMapper.prPricebyFpId(fpId);
		
		
		if(list != null && list.size()>0){
			prPriceTO = list.get(0);
			
			map.put("prPrice", prPriceTO);
			
			//查询会员信息
    		Map<String,String> params = new HashMap<String,String>();
    		params.put("HQId", prPriceTO.getHqId());
    		params.put("openId", openId);
    		// 调用接口创建菜单
    		
        	String response = httpManager.getHttpsResponseByPost(Constant.QUERY_MEMBER_URL, params, "utf-8");
        	List<MemberVO> memberArray = listMember(response);
        	if(memberArray != null && memberArray.size()>0){
        		map.put("member", memberArray.get(1));
        	}else{
        		throw new Exception("没有找到当前油站对应的会员！");
        	}
			
		}
		return map;
	}
	
	
	
	/**
	 * 根据openId得到客户下的所有账户
	 */
	public List<MemberVO> listMember(String result) throws Exception {
		List<MemberVO>list = new ArrayList<MemberVO>();
		
		JSONObject json = new JSONObject(result);
		if(!json.isNull("code")){
			String codes = json.getString("code");
			if(codes.equals("0")){
				JSONObject data = json.getJSONObject("data");
				String str = data.getString("list");
				list = gson.fromJson(str,new TypeToken<List<MemberVO>>(){}.getType());
				
			}
		}
		return list;
	}

	@Override
	public String authorization(AuthVO authVO) throws Exception {
		String response = null;
		//查询会员信息
		Map<String,String> params = new HashMap<String,String>();
		params.put("params", gson.toJson(authVO));
		// 调用接口创建菜单
		try{
			response = httpManager.getHttpsResponseByPost(Constant.AUTH_URL, params, "utf-8");
			
			JSONObject json = new JSONObject(response);
			if(!json.isNull("code")){
				String codes = json.getString("code");
				if(!codes.equals("200")){
					throw new Exception(json.getString("msg"));
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			throw new Exception("授权失败");
		}
		
		return null;
	}

	@Override
	public List<MemberPasswordTO> passwordByMemberId(String memberId,
			String password) throws Exception {
		return memberPasswordMapper.passwordByMemberId(memberId, MD5Util.encode(password));
	}


}
